package com.time.hellotime.components.discovery;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.hellotime.R;
import com.time.hellotime.base.BaseViewHolder;
import com.time.hellotime.base.BaseXRecAdapter;
import com.time.hellotime.servermodel.discovery.Discovery;
import com.time.hellotime.utils.ScreenUtils;
import com.time.hellotime.utils.TimeUtils;
import com.time.hellotime.webserver.helper.picasso.PicassoImageLoader;

import java.util.List;

/**
 * 发现公益 - 已完成
 * 我的志愿 - 已完成
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class OverAdapter extends BaseXRecAdapter<Discovery> {

    private boolean isVolunteer;
    private String mNowTime;

    public void setNowTime(String nowTime) {
        this.mNowTime = nowTime;
    }

    public OverAdapter(Context mContext, List<Discovery> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View convertView = inflater.inflate(R.layout.item_over_list, parent, false);
        ExeViewHolder exeViewHolder = new ExeViewHolder(convertView);
        convertView.setTag(exeViewHolder);
        return exeViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position, boolean isItem) {
        final Discovery discovery = getItem(position);
        ExeViewHolder exeHolder = (ExeViewHolder) holder;
        if (!TextUtils.isEmpty(discovery.getCoverPicUrl())) {
            int width = ScreenUtils.dip2px(92);
            int height = ScreenUtils.dip2px(68);
            PicassoImageLoader.loadImageViewSize(
                    mContext,
                    discovery.getCoverPicUrl(),
                    width,
                    height,
                    R.drawable.iv_default_92_68,
                    R.drawable.iv_default_92_68,
                    toString(), exeHolder.ivPic);
        }
        exeHolder.tvLocation.setText(discovery.getCity());
        exeHolder.tvTitle.setText(discovery.getTitle());
        if (isVolunteer){
            exeHolder.tvSubTitle.setText("已完成");
        } else {
            if (!TextUtils.isEmpty(discovery.getTotal_time())){
                long distanceTimes = Long.parseLong(discovery.getTotal_time());
                String format = TimeUtils.formatTimeXx(distanceTimes);
                exeHolder.tvSubTitle.setText(new StringBuilder()
                        .append("累积公益时间：")
                        .append(format));
            } else {
                exeHolder.tvSubTitle.setText(new StringBuilder()
                        .append("累积公益时间：")
                        .append("00h00m00s"));
            }
        }

        if (position == mList.size() - 1) {
            exeHolder.divider.setVisibility(View.GONE);
        } else {
            exeHolder.divider.setVisibility(View.VISIBLE);
        }

        exeHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener) {
                    if (TextUtils.isEmpty(discovery.getId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, discovery, getViewType());
                    }
                }
            }
        });

    }

    class ExeViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public ImageView ivPic;
        public TextView tvLocation;
        public TextView tvTitle;
        public TextView tvSubTitle;
        public View divider;

        public ExeViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_over_item_parent);
            ivPic = getView(R.id.iv_over_pic);
            tvLocation = getView(R.id.tv_over_location);
            tvTitle = getView(R.id.tv_over_title);
            tvSubTitle = getView(R.id.tv_over_sub_title);
            divider = getView(R.id.v_divider);
        }
    }

    public void setVolunteer(boolean volunteer) {
        isVolunteer = volunteer;
    }
}
